package synchronizeds;

/*
     volatile和synchronized区别
     https://www.jianshu.com/p/5446cd9a91a4

 */
public class SingleTon {
    private volatile static SingleTon mSingleTon;
    //这里添加volatile，可以保存在多线程里有序性，synchronized在单线程里可以保证有序性
    //volatile可以避免指令重排， synchronized是无法禁止指令重排和处理器优化的

    public SingleTon getInstance(){
        if (mSingleTon == null){
            synchronized (SingleTon.class){
                if (mSingleTon == null){
                    mSingleTon = new SingleTon();
                }
            }
        }

        return mSingleTon;
    }
}
